package com.Dao;

import com.bean.Remark;
import com.bean.Words;
import com.tools.DButil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class WordsDao {
    //添加留言
    public void addWords(Words words)throws SQLException {
        String sql="insert into words(author,pubtime,content,reContent) value(?,?,?,?)";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        runner.update(sql,words.getAuthor(),words.getPubtime(),words.getContent(),words.getReContent());
    }
    //删除留言
    public void deleteWords(int id) throws SQLException {
        String sql="delete from words where id=?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        runner.update(sql,id);
    }

    //显示所有评论
    public List<Words> getAllWords() throws SQLException {
        String sql = "select * from words order by pubtime desc limit 0,5";//根据时间降序，limit0，10——是指分页，10个数据一页
        QueryRunner runner = new QueryRunner(DButil.getDataSource());
        return runner.query(sql, new BeanListHandler<Words>(Words.class));
    }

    //获取评论的发布时间
    public Date findPubTime(String content) throws SQLException {
        String sql="select pubtime from words where content=?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        Date time= (Date) runner.query(sql,new ScalarHandler(),content);
        return time;

    }
    //显示条数
    public int findAllCount() throws SQLException {
        String sql="SELECT COUNT(*) FROM words";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        Long count=(Long) runner.query(sql,new ScalarHandler());
        return count.intValue();

    }
    public List<Words> findWordsById(int id) throws SQLException {
        String sql="SELECT * FROM words where id =?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        return runner.query(sql, new BeanListHandler<Words>(Words.class),id);


    }


    //回复评论
}
